// 引入vuex模块
import { createStore } from 'vuex';

// 使用创建函数创建vuex对象
export default createStore({
    // state: 状态（需要在系统中用到的数据）
    state: {
        name: "Hydra-Store",
        mobile: "13547811112",
        stus: [
            {name: "刘煜鑫",sex: "男",age: 17},
            {name: "孔小琴",sex: "女",age: 17},
            {name: "孙孜子错",sex: "男",age: 38},
            {name: "李红梅",sex: "女",age: 20},
        ],
        // 当前登录用户
        user: {},
        messages: []
    },
    mutations: {
        // setName有两个参数
        //  state：当前的state
        //  payload：提交需要更改的数据
        setName(state,payload) {
            state.name = payload;
        },
        setSchool(state,payload) {
            state.school = payload;
        },
        setMobile(state,payload) {
            state.mobile = payload;
        },
        setSex(state,payload) {
            state.sex = payload;
        },
        setUser(state,payload) {
            state.user = payload;
        }
    },
    getters: {
        getGirls(state) {
            return state.stus.filter(v => v.sex == "女");
        },
        getAdultGirls: (state,getters) => getters.getGirls.filter(v => v.age >= 18),
        getBoys: state => state.stus.filter(v => v.sex == "男"),
        getBiggerThan: state => age => state.stus.filter(v => v.age > age),
        getLessThan(state) {
            return function(age) {
                return state.stus.filter(v => v.age <= age);
            }
        }
    },
    actions: {
        actionName(context,payload) {
            console.log("context:",context)
            // 可以进行异步提交
            setTimeout(() => {            
                context.commit("setName",payload);
            },1000)
            
        },
        actionSchool({commit},payload) {
            setTimeout(() => {            
                commit("setSchool",payload);
            },1000)
        }
    }
});